package com.turnengine.client.local.action.command;

import com.javabi.command.IExecutableCommand;
import com.javabi.command.IExecutableCommandResponse;
import com.javabi.command.serializer.AbstractCommandDataSerializer;
import com.javabi.common.io.data.IDataReader;
import com.javabi.common.io.data.IDataWriter;
import com.turnengine.client.local.action.bean.ActionSerializer;
import com.turnengine.client.local.action.bean.IAction;
import java.io.IOException;

/**
 * The Add Action Data Serializer.
 */
public class AddActionDataSerializer extends AbstractCommandDataSerializer<IAction> implements IAddActionDataSerializer {

	/**
	 * Creates a new Add Action Data Serializer.
	 */
	public AddActionDataSerializer() {
		super(AddAction.COMMAND_ID);
	}

	@Override
	public IAction readReturnValue(IDataReader reader) throws IOException {
		IAction value = new ActionSerializer().readObject(reader);
		return value;
	}

	@Override
	public void writeReturnValue(IDataWriter writer, IAction value) throws IOException {
		new ActionSerializer().writeObject(writer, value);
	}

	@Override
	public IExecutableCommand<IAction> readRequest(IDataReader reader) throws IOException {

		// Read fields
		long loginId = reader.readLong();
		int instanceId = reader.readInt();
		int actionId = reader.readInt();
		int turns = reader.readInt();
		int limit = reader.readInt();

		// Create object
		AddAction object = new AddAction();
		object.setLoginId(loginId);
		object.setInstanceId(instanceId);
		object.setActionId(actionId);
		object.setTurns(turns);
		object.setLimit(limit);
		return object;
	}

	@Override
	public void writeRequest(IDataWriter writer, IExecutableCommand<IAction> argument) throws IOException {
		AddAction object = (AddAction) argument;

		// Get fields
		long loginId = object.getLoginId();
		int instanceId = object.getInstanceId();
		int actionId = object.getActionId();
		int turns = object.getTurns();
		int limit = object.getLimit();

		// Write fields
		writer.writeLong(loginId);
		writer.writeInt(instanceId);
		writer.writeInt(actionId);
		writer.writeInt(turns);
		writer.writeInt(limit);
	}

	@Override
	public IExecutableCommandResponse<IAction> newResponse() {
		return new AddActionResponse();
	}
}
